package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.TourSmallCity;
import com.ruoyi.system.domain.vo.TourSmallPackage;
import com.ruoyi.system.domain.vo.TourSmallWorker;
import com.ruoyi.system.mapper.TourGroupMapper;
import com.ruoyi.system.service.ITourGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TourGroupService implements ITourGroupService {
    @Autowired
    private TourGroupMapper groupMapper;
    // 用于日期格式化
    public static final SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<TourGroup> selectGroupList() {
        return groupMapper.selectGroupList();
    }

    @Override
    public TourGroup selectGroupDetail(int id) {
        TourGroup group = groupMapper.selectGroupDetail(id);
        // 查询团队中的团员信息
        if (group != null){
            List<TourGroupMember> members = groupMapper.selectGroupMember(id);
            group.setMembers(members);
        }
        return group;
    }

    @Override
    public void deleteGroup(int id) {
        groupMapper.deleteGroup(id);
    }

    @Override
    public void createGroup(TourGroup group) {
        groupMapper.createGroup(group);
        //插入人员信息
        int new_group_id = groupMapper.selectGroupId();
        group.setId(new_group_id);
//        System.out.println(new_group_id);
        for (TourGroupMember member : group.getMembers()){
            groupMapper.addMember(member,group.getId());
        }
    }

    @Override
    public void updateGroup(TourGroup group) {
        groupMapper.updateGroup(group);
        // 删除该团队中所有的成员
        groupMapper.deleteAllMember(group.getId());
        // 向团队中插入人员
        for (TourGroupMember member : group.getMembers()){
            groupMapper.addMember(member,group.getId());
        }
    }

    @Override
    public List<TourSmallPackage> selectPackageOfCity(int id) {
        return groupMapper.selectPackageOfCity(id);
    }

    @Override
    public List<TourSmallWorker> selectWorkerOfGroup() {
        List<TourSmallWorker> workers = groupMapper.selectWorkerOfGroup();
        return workers;
    }
}
